package cn.szzsi._web;


import cn.szzsi.common.bean.dto.Msg;

import cn.szzsi.common.kit.SessionUtil;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.Tocash;
import cn.szzsi.common.model.TocashBank;
import cn.szzsi.service.UserBalanceService;
import cn.szzsi.service.UserService;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.weixin.sdk.api.ApiConfigKit;

import java.math.BigDecimal;
import java.util.List;

/**
 * 提现
 * Created by yuanzheng on 2016/11/25.
 */
public class CashingController extends BaseWebController<Tocash>{
    private String shopId;//用户的shopId就是商户的id
    private String path;//模板路径

    //会员提现记录
    public void list(){
        shopId= getPara("shopId");
        Member member=getLoginMember();
        List<Tocash> toCashingList= Tocash.getByMemberId(member.getStr("id"));
        setAttr("toCashingList",toCashingList);
        wapTemplatePath("tocashing_list.html");

    }
    public void edit(){
        shopId= getPara("shopId");
        Member member=getLoginMember();
        if(member.getStr("memberRankId")!=null && member.getStr("memberRankId").equalsIgnoreCase("sf60982c5faf48cb8f59a492a73a6b2c")){
            renderHtml("<script>alert('准会员不能提现');location.href='/b/customerCenter/capital?shopId="+shopId+"';</script>");
            return;
        }
        //如果该人未绑定手机号码和身份证信息，则跳转到绑定信息页面
        if(StrKit.isBlank(member.getStr("phone"))){
            renderHtml("<script>alert('请先绑定手机号码，才能继续提现');location.href='/b/customerCenter/phoneapply?id="+member.getStr("id")+"&shopId="+shopId+"';</script>");
            return;
        }

        Tocash toCashing= Tocash.getLastByMemberId(member.getStr("id"));//最后一次提现记录
        int lastBankId;
        if(toCashing!=null){
            lastBankId=toCashing.getInt("bankId");
            TocashBank toCashingBank= TocashBank.dao.findById(lastBankId);
            setAttr("toCashingBank",toCashingBank);
        }
        String bankId=getPara("bankId");
        if(StrKit.notBlank(bankId)){
            setAttr("selectCashingBank", TocashBank.dao.findById(bankId));
        }
        setAttr("deposit", UserBalanceService.service.getAvailBalance(member));//可转出到银行卡的余额=总金额-冻结金额
        wapTemplatePath("tocashing_input.html");
    }

    /**
     * 买家发起提现申请,还有当上一笔订单申请通过,该笔订单才能发起申请
     */
    public void save(){
        Member member=getLoginMember();
        Tocash toCashing= Tocash.getLastByMemberId(member.getStr("id"));//上一笔订单
        if(toCashing!=null && toCashing.getInt("status")== Tocash.ToCashingStatus.inToCashing.ordinal()){//最后一笔订单在提现中
            renderJson(Msg.fail("亲,您有一笔提现还未审核通过哦!",""));
            return;
        }
        String bankId=getPara("bankId");//银行卡id
        String cashAmount=getPara("cashAmount");//提现金额
        BigDecimal deposit=UserBalanceService.service.getAvailBalance(member);//可提余额
        BigDecimal bdCashAmount=new BigDecimal(cashAmount);
        if(bdCashAmount.compareTo(deposit)>0){
            renderJson(Msg.fail("亲,您的账户余额为"+deposit.toString()+"元,最多只能提取"+deposit.toString()+"元哦!",""));
            return;
        }
        if(bdCashAmount.compareTo(new BigDecimal(10))<0){
            renderJson(Msg.fail("亲,提现金额不能小于10元哦!",""));
            return;
        }
        Tocash.saveToCashing(member.getId(),member.getShopId(),bankId,"",cashAmount);
        renderJson(Msg.SUCCESS);

    }

    private BigDecimal getAvailDeposit(Member member){
        BigDecimal deposit = null;
        long num = Db.queryLong("select count(*) from mall_member where pid = '"+member.getStr("id")+"'");
        if(num == 1){
            deposit = Db.queryColumn("select sum(money) from mall_backmoney where uid = ? and type = 'FX' and level = 1",member.get("id"));
        }else if(num == 2){
            deposit = Db.queryColumn("select sum(money) from mall_backmoney where uid = ? and type = 'FX' and (level = 1 or level = 2)",member.get("id"));
        }else if(num >2){
            deposit = Db.queryColumn("select sum(money) from mall_backmoney where uid = ? and type = 'FX'",member.get("id"));
        }
        if(deposit == null)
            deposit = new BigDecimal(0);

        return deposit;
    }


}
